<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <!-- 主表单区域 -->
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="24" >
            <a-form-model-item label="班次名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="className">
              <a-input v-model="model.className" placeholder="请输入班次名称" ></a-input>
            </a-form-model-item>
          </a-col>
<!--          <a-col :span="24" >-->
<!--            <a-form-model-item label="部门" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="departId">-->
<!--              <j-select-depart v-model="model.departId" :multi="true"  />-->
<!--            </a-form-model-item>-->
<!--          </a-col>-->
          <a-col :span="24" >
            <a-form-model-item label="备注" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="remark">
              <a-input v-model="model.remark" placeholder="请输入备注" ></a-input>
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
      <!-- 子表单区域 -->
    <a-tabs v-model="activeKey" @change="handleChangeTabs">
      <a-tab-pane tab="线路配置" :key="refKeys[0]" :forceRender="true">
        <j-vxe-table
          keep-source
          :ref="refKeys[0]"
          :loading="insClassesLineTable.loading"
          :columns="insClassesLineTable.columns"
          :dataSource="insClassesLineTable.dataSource"
          :maxHeight="300"
          :disabled="formDisabled"
          :rowNumber="true"
          :rowSelection="true"
          :toolbar="true"
          />
      </a-tab-pane>
      <a-tab-pane tab="班次人员" :key="refKeys[1]" :forceRender="true">
        <j-vxe-table
          keep-source
          :ref="refKeys[1]"
          :loading="insClassesUserTable.loading"
          :columns="insClassesUserTable.columns"
          :dataSource="insClassesUserTable.dataSource"
          :maxHeight="300"
          :disabled="formDisabled"
          :rowNumber="true"
          :rowSelection="true"
          :toolbar="true"
          />
      </a-tab-pane>
      <a-tab-pane tab="正常日期" :key="refKeys[2]" :forceRender="true">
        <j-vxe-table
          keep-source
          :ref="refKeys[2]"
          :loading="insClassesCommonTable.loading"
          :columns="insClassesCommonTable.columns"
          :dataSource="insClassesCommonTable.dataSource"
          :maxHeight="300"
          :disabled="formDisabled"
          :rowNumber="true"
          :rowSelection="true"
          :toolbar="true"
          />
      </a-tab-pane>
      <a-tab-pane tab="特殊日期" :key="refKeys[3]" :forceRender="true">
        <j-vxe-table
          keep-source
          :ref="refKeys[3]"
          :loading="insClassesSpecialTable.loading"
          :columns="insClassesSpecialTable.columns"
          :dataSource="insClassesSpecialTable.dataSource"
          :maxHeight="300"
          :disabled="formDisabled"
          :rowNumber="true"
          :rowSelection="true"
          :toolbar="true"
          />
      </a-tab-pane>
    </a-tabs>
  </a-spin>
</template>

<script>

  import { getAction } from '@/api/manage'
  import { JVxeTableModelMixin } from '@/mixins/JVxeTableModelMixin.js'
  import { JVXETypes } from '@/components/jeecg/JVxeTable'
  import { getRefPromise,VALIDATE_FAILED} from '@/components/jeecg/JVxeTable/utils/vxeUtils.js'
  import { validateDuplicateValue } from '@/utils/util'
  import JFormContainer from '@/components/jeecg/JFormContainer'

  export default {
    name: 'InsClassesForm',
    mixins: [JVxeTableModelMixin],
    components: {
      JFormContainer,
    },
    data() {
      return {
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        model:{
         },
        // 新增时子表默认添加几行空数据
        addDefaultRowNum: 1,
        validatorRules: {
        },
        refKeys: ['insClassesLine', 'insClassesUser', 'insClassesCommon', 'insClassesSpecial', ],
        tableKeys:['insClassesLine', 'insClassesUser', 'insClassesCommon', 'insClassesSpecial', ],
        activeKey: 'insClassesLine',
        // 线路配置
        insClassesLineTable: {
          loading: false,
          dataSource: [],
          columns: [
            {
              title: '线路ID',
              key: 'lineId',
               type: JVXETypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
            {
              title: '备注',
              key: 'remark',
               type: JVXETypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
          ]
        },
        // 班次人员
        insClassesUserTable: {
          loading: false,
          dataSource: [],
          columns: [
            {
              title: '人员',
              key: 'userId',
              type: JVXETypes.userSelect,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
            {
              title: '备注',
              key: 'remark',
               type: JVXETypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
          ]
        },
        // 正常日期
        insClassesCommonTable: {
          loading: false,
          dataSource: [],
          columns: [
            {
              title: '规则名称',
              key: 'classCommonName',
               type: JVXETypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
            // {
            //   title: '部门',
            //   key: 'departId',
            //   type: JVXETypes.departSelect,
            //   width:"200px",
            //   placeholder: '请输入${title}',
            //   defaultValue:'',
            // },
            {
              title: '打卡签到起始时间',
              key: 'classTimePre',
               type: JVXETypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
            {
              title: '打卡签到结束时间',
              key: 'classTimeEnd',
               type: JVXETypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
            {
              title: '巡检次数',
              key: 'classPollNum',
               type: JVXETypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
            {
              title: '备注',
              key: 'remark',
               type: JVXETypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
          ]
        },
        // 特殊日期
        insClassesSpecialTable: {
          loading: false,
          dataSource: [],
          columns: [
            {
              title: '规则名称',
              key: 'classSpecialName',
               type: JVXETypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
            // {
            //   title: '部门',
            //   key: 'departId',
            //   type: JVXETypes.departSelect,
            //   width:"200px",
            //   placeholder: '请输入${title}',
            //   defaultValue:'',
            //   validateRules: [{ required: true, message: '${title}不能为空' }],
            // },
            {
              title: '执行时间',
              key: 'classExecuteTime',
              type: JVXETypes.date,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
            {
              title: '打卡签到起始时间',
              key: 'classTimePre',
               type: JVXETypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
            {
              title: '打卡签到结束时间',
              key: 'classTimeEnd',
               type: JVXETypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
            {
              title: '巡检次数',
              key: 'classPollNum',
               type: JVXETypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
            {
              title: '备注',
              key: 'remark',
               type: JVXETypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
          ]
        },
        url: {
          add: "/inspect/insClasses/add",
          edit: "/inspect/insClasses/edit",
          queryById: "/inspect/insClasses/queryById",
          insClassesLine: {
            list: '/inspect/insClasses/queryInsClassesLineByMainId'
          },
          insClassesUser: {
            list: '/inspect/insClasses/queryInsClassesUserByMainId'
          },
          insClassesCommon: {
            list: '/inspect/insClasses/queryInsClassesCommonByMainId'
          },
          insClassesSpecial: {
            list: '/inspect/insClasses/queryInsClassesSpecialByMainId'
          },
        }
      }
    },
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    computed: {
      formDisabled(){
        return this.disabled
      },
    },
    created () {
    },
    methods: {
      addBefore(){
        this.insClassesLineTable.dataSource=[]
        this.insClassesUserTable.dataSource=[]
        this.insClassesCommonTable.dataSource=[]
        this.insClassesSpecialTable.dataSource=[]
      },
      getAllTable() {
        let values = this.tableKeys.map(key => getRefPromise(this, key))
        return Promise.all(values)
      },
      /** 调用完edit()方法之后会自动调用此方法 */
      editAfter() {
        this.$nextTick(() => {
        })
        // 加载子表数据
        if (this.model.id) {
          let params = { id: this.model.id }
          this.requestSubTableData(this.url.insClassesLine.list, params, this.insClassesLineTable)
          this.requestSubTableData(this.url.insClassesUser.list, params, this.insClassesUserTable)
          this.requestSubTableData(this.url.insClassesCommon.list, params, this.insClassesCommonTable)
          this.requestSubTableData(this.url.insClassesSpecial.list, params, this.insClassesSpecialTable)
        }
      },
      //校验所有一对一子表表单
        validateSubForm(allValues){
            return new Promise((resolve,reject)=>{
              Promise.all([
              ]).then(() => {
                resolve(allValues)
              }).catch(e => {
                if (e.error === VALIDATE_FAILED) {
                  // 如果有未通过表单验证的子表，就自动跳转到它所在的tab
                  this.activeKey = e.index == null ? this.activeKey : this.refKeys[e.index]
                } else {
                  console.error(e)
                }
              })
            })
        },
      /** 整理成formData */
      classifyIntoFormData(allValues) {
        let main = Object.assign(this.model, allValues.formValue)
        return {
          ...main, // 展开
          insClassesLineList: allValues.tablesValue[0].tableData,
          insClassesUserList: allValues.tablesValue[1].tableData,
          insClassesCommonList: allValues.tablesValue[2].tableData,
          insClassesSpecialList: allValues.tablesValue[3].tableData,
        }
      },
      validateError(msg){
        this.$message.error(msg)
      },

    }
  }
</script>

<style scoped>
</style>